package com.doctor.adapter;

import java.util.List;

import com.doctor.bean.BookServiceBean;
import com.doctor.ui.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BookServiceAdapter extends BaseAdapter{
	
	/**
	 * 预约服务
	 */
	
	private Context context;
	private List<BookServiceBean> beanList;
	private BookServiceAdapterCallback callback;
	
	
	public BookServiceAdapter(Context context, List<BookServiceBean> beanList, BookServiceAdapterCallback callback){
		this.context=context;
		this.beanList=beanList;
		this.callback=callback;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beanList.size();
	}

	@Override
	public BookServiceBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return beanList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (arg1==null) {
			holder=new ViewHolder();
			arg1=View.inflate(context, R.layout.book_service_list_item, null);			
			holder.hospitalDepartment=(TextView) arg1.findViewById(R.id.hospitalDepartment);
			holder.phone=(TextView) arg1.findViewById(R.id.phone);
			holder.specialNum=(TextView) arg1.findViewById(R.id.specialNum);
			holder.webSite=(TextView) arg1.findViewById(R.id.webSite);
			
			arg1.setTag(holder);
		} else {
			holder=(ViewHolder) arg1.getTag();
		}
		
		BookServiceBean bean=beanList.get(arg0);
		
		holder.hospitalDepartment.setText(bean.getHospital());
		
		holder.phone.setText(bean.getTel());
		holder.phone.setTag(bean.getTel());
		holder.phone.setOnClickListener(onClick);
		
		holder.specialNum.setText(bean.getGh());
		holder.specialNum.setTag(bean.getGh());
		holder.specialNum.setOnClickListener(onClick);
		
		holder.webSite.setText(bean.getWebsite());
		holder.webSite.setTag(bean.getWebsite());
		holder.webSite.setOnClickListener(onClick);
		
		arg1.setTag(R.id.bean_tag, bean);
		return arg1;
	}
	
	
	
	private class ViewHolder{
		TextView hospitalDepartment;
		TextView phone;
		TextView specialNum;
		TextView webSite;
	}
	
	
	
	private OnClickListener onClick=new OnClickListener() {		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			String txt=(String) arg0.getTag();
			switch (arg0.getId()) {
			case R.id.phone:
				callback.onPhoneClicked(txt);
				break;

			case R.id.specialNum:
				callback.onSpecialNumClicked(txt);
				break;
				
			case R.id.webSite:
				callback.onWebSiteClicked(txt);
				break;	
			}
		}
	};
	
	
	public interface BookServiceAdapterCallback{
		public void onPhoneClicked(String phone);
		public void onSpecialNumClicked(String specialNum);
		public void onWebSiteClicked(String webSite);
	}
	
	
	public void clear(){
		beanList.clear();
	}
	
	
	public void remove(BookServiceBean bean){
		boolean result=beanList.remove(bean);
		if (result) {
			notifyDataSetChanged();
		}
	}
	
	
	
	public void add(List<BookServiceBean> list){
		boolean result= beanList.addAll(list);
		if (result) {
			notifyDataSetChanged();
		}		
	}
	

}
